//
//  CNoticeMgr.m
//  WZEfengAndEtong
//
//  Created by 李旭 on 16/6/1.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "CNoticeMgr.h"
#import "CNoticeModel.h"
@implementation CNoticeMgr
-(BOOL)insertNotice:(CNoticeModel *)notice
{
    if(notice == nil || M_ISSTRNULL(notice.noticeId))
    {
        return NO;
    }
    
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
    ret = [db executeUpdate:@"INSERT INTO NOTICE(ServerID, NoticeType, NoticeContent, ReadStatus, SoundPlayStatus, GroupId, SenderCode,MineJid,FriendJid, TimeStamp , NoticeId,MineUserId,FriendUserId,isMineSend,FriendUserName,MineUserName,IsShowInmessageList,Stream, SendStatus,yuLiuZiduan1, yuLiuZiduan2, yuLiuZiduan3) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", M_STOREVALUE(notice.serverID), M_STOREVALUE(notice.noticeType), M_STOREVALUE(notice.noticeContent) , notice.readStatus?@1:@0, @(notice.soundPlayStatus), M_STOREVALUE(notice.GroupId), M_STOREVALUE(notice.SenderCode),M_STOREVALUE(notice.MineJid),M_STOREVALUE(notice.FriendJid),M_STOREVALUE(notice.TimeStamp),M_STOREVALUE(notice.noticeId),M_STOREVALUE(notice.MineUserId),M_STOREVALUE(notice.FriendUserId),notice.isMineSend?@1:@0,M_STOREVALUE(notice.FriendUserName),M_STOREVALUE(notice.MineUserName),notice.IsShowInmessageList?@1:@0,M_STOREVALUE(notice.Stream),M_STOREVALUE(notice.SendStatus),M_STOREVALUE(notice.yuLiuZiduan1),@(notice.yuLiuZiduan2),M_STOREVALUE(notice.yuLiuZiduan3)];
        
    }];
    return ret;

}
-(BOOL)updataNoticeReadStatusWithNoticeId:(NSString *)noticeId
{
    if(M_ISSTRNULL(noticeId))
    {
        return NO;
    }
    
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE NOTICE SET  ReadStatus = 1 where NoticeId = ? ",M_STOREVALUE(noticeId)];
    }];
    return ret;
}
-(NSInteger)loadAllUnReadCount
{
    NSString *sql = nil;
    
    sql = [NSString stringWithFormat:@"select COUNT(*) from NOTICE where ReadStatus = 0 AND isMineSend = 0"];
    __block NSInteger count;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        count = [db intForQuery:sql];
    }];
    return count;

}
-(BOOL)batchInsertNotice:(NSArray *)noticeList
{
    if(noticeList == nil || [noticeList count] <= 0)
    {
        return NO;
    }
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(CNoticeModel *notice in noticeList)
        {
            if (notice && !M_ISSTRNULL(notice.noticeId)) {
                NSString *result = [db stringForQuery:@"select TimeStamp from NOTICE where NoticeId = ?",notice.noticeId];
                
                if (!M_ISSTRNULL(result)) {
                    ret = YES;
                } else {
                    ret = [db executeUpdate:@"INSERT INTO NOTICE(ServerID, NoticeType, NoticeContent, ReadStatus, SoundPlayStatus, GroupId, SenderCode,MineJid,FriendJid, TimeStamp , NoticeId,MineUserId,FriendUserId,isMineSend,FriendUserName,MineUserName,IsShowInmessageList,Stream, SendStatus,yuLiuZiduan1, yuLiuZiduan2, yuLiuZiduan3) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", M_STOREVALUE(notice.serverID), M_STOREVALUE(notice.noticeType), M_STOREVALUE(notice.noticeContent) , notice.readStatus?@1:@0, @(notice.soundPlayStatus), M_STOREVALUE(notice.GroupId), M_STOREVALUE(notice.SenderCode),M_STOREVALUE(notice.MineJid),M_STOREVALUE(notice.FriendJid),M_STOREVALUE(notice.TimeStamp),M_STOREVALUE(notice.noticeId),M_STOREVALUE(notice.MineUserId),M_STOREVALUE(notice.FriendUserId),notice.isMineSend?@1:@0,M_STOREVALUE(notice.FriendUserName),M_STOREVALUE(notice.MineUserName),notice.IsShowInmessageList?@1:@0,M_STOREVALUE(notice.Stream),M_STOREVALUE(notice.SendStatus),M_STOREVALUE(notice.yuLiuZiduan1),@(notice.yuLiuZiduan2),M_STOREVALUE(notice.yuLiuZiduan3)];
                }

            }
            
            if(!ret)
            {
                *rollback = YES;
            }
        }
        
    }];
    return ret;

}
@end
